const app = getApp()
const util = require('../util/util.js')

Page({
  data: {
    bannerList: [],
    classArr: ["left_item", "active_item", "right_item", "hidden_item"],
    categoryList: [],
    gold: 0,
    check: false, // 审核控制器，false - 审核版本
    check_info: null, // 
    isIos: false,
    more_title: '游戏热榜',
    more_list: []
  },

  onLoad: function() {
    let that = this
    that.getConfig()
    wx.request({
      url: app.globalData.url + 'index',
      success: function(res) {
        let data = res.data.data
        that.setData({
          more_title: data.more.title,
          more_list: data.more.list,
          bannerList: data.banner,
          categoryList: data.category,
        })
      }
    })
    that.changeBanner()
  },

  onShow: function() {
    this.setData({
      gold: app.globalData.gold,
    })
  },

  // 获取全局配置config
  getConfig: function() {
    let that = this
    wx.request({
      url: app.globalData.url + 'config',
      data: {
        version: app.globalData.version,
        appid: app.globalData.appid
      },
      success: (res) => {
        wx.getSystemInfo({
          success: (result) => {
            let systemInfo = result.system
            if (systemInfo.indexOf("iOS") != -1) {
              app.globalData.isUserSystemIos = true
            }
          },
        })

        // 根据开关显示是否是审核内容
        that.setData({
          check: res.data.data.check,
          isIos: res.data.data.ios_check
        })
        if (res.data.data.check) { // check: true => 开启审核
          wx.request({
            url: app.globalData.url + 'fake_info',
            success: (res) => {
              that.setData({
                check_info: res.data.data
              })
            },
          })
        } else {
          app.editTabbar()
        }

      }
    })
  },

  onShareAppMessage: function() {
    var taskList = util.getTaskList()
    if (!taskList.share) {
      taskList.share = 1
      util.setTaskList(taskList)
    }
  },

  // 跳转小程序(判断是否有二维码图片,有则展示)
  goMiniProgram: function(e) {
    let game = e.currentTarget.dataset.game
    console.log(game);
    let list = wx.getStorageSync('baibao_game_history')
    list.push(game)
    if (game.click_type == 2) {
      wx.previewImage({
        current: game.code_url,
        urls: [game.code_url]
      })
      wx.setStorageSync('baibao_game_history', list)
    } else if (game.click_type == 1) {
      wx.navigateToMiniProgram({
        appId: game.appid,
        path: game.path,
        extraData: game.extra_data,
        success(res) {
          wx.setStorageSync('baibao_game_history', list)
        }
      })
    } else if (game.click_type == 3) {
      wx.navigateToMiniProgram({
        appId: game.belong,
        path: '/pages/index/index?appid=' + game.appid,
        extraData: game.extra_data,
        success(res) {
          wx.setStorageSync('baibao_game_history', list)
        }
      })
    }
  },

  // 自动切换banner图
  changeBanner: function() {
    setInterval(() => {
      var arr1 = this.data.classArr[this.data.classArr.length - 1]
      this.data.classArr.unshift(arr1)
      this.data.classArr.pop()
      this.setData({
        classArr: this.data.classArr
      })
    }, 3000)
  },

  // 手势切换banner图
  touchstart: function(e) {
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY
    })
  },
  touchend: function(e) {
    var that = this
    this.setData({
        endX: e.changedTouches[0].clientX,
        endY: e.changedTouches[0].clientY
      }),
      this.getTouchData(e.changedTouches[0].clientX, e.changedTouches[0].clientY, that.data.startX, that.data.startY)
  },
  getTouchData: function(sx, sy, ex, ey) {
    if ((ex - sx) > 50 && Math.abs(sy - ey) < 50) {
      var arr1 = this.data.classArr[this.data.classArr.length - 1]
      this.data.classArr.unshift(arr1)
      this.data.classArr.pop()
      this.setData({
        classArr: this.data.classArr
      })
    }
    if ((sx - ex) > 50 && Math.abs(sy - ey) < 50) {
      var arr1 = this.data.classArr[0]
      this.data.classArr.shift()
      this.data.classArr.push(arr1)
      this.setData({
        classArr: this.data.classArr
      })
    }
  },
})